﻿using Microsoft.EntityFrameworkCore;
using NewRetail.Application.Components.Warehouse.Entity;
using NewRetail.Application.Core.Entity;

namespace NewRetail.Application.Components.Warehouse
{
    public static class WarehouseExtension
    {
        public static void ConfigureWarehouseEntities(this ModelBuilder modelBuilder)
        {
            var discountBuilder = modelBuilder.Entity<PromoteSalesDiscount>()
                .ConfigBaseEntity().ConfigReceiptEntity().ConfigWarehouse();
            discountBuilder.ToTable("promote_sale_discount");
            discountBuilder
                .Ignore(x => x.Warehouse)
                .Ignore(x => x.GoodsCategory)
                .Ignore(x => x.PromoteSalesDiscountCategory)
                .Ignore(x => x.PromoteSalesDiscountGoods)
                .Ignore(x => x.GoodsInfo);

            var discountCategoryBuilder = modelBuilder.Entity<PromoteSalesDiscountCategory>().ConfigBaseEntity();
            discountCategoryBuilder.ToTable("promote_sale_discount_category");
            discountCategoryBuilder.Property(x => x.discountID).IsRequired().HasMaxLength(100);
            discountCategoryBuilder.Property(x => x.categoryID).IsRequired().HasMaxLength(100);
            discountCategoryBuilder.Ignore(x => x.GoodsCategory).Ignore(x => x.PromoteSalesDiscount);

            //单品打折
            var discountGoodsBuilder = modelBuilder.Entity<PromoteSalesDiscountGoods>().ConfigBaseEntity();
            discountGoodsBuilder.ToTable("promote_sale_discount_goods");
            discountGoodsBuilder.Property(x => x.discountID).IsRequired().HasMaxLength(100);
            discountGoodsBuilder.Property(x => x.goodsID).IsRequired().HasMaxLength(100);
            discountGoodsBuilder.Ignore(x => x.GoodsInfo).Ignore(x => x.PromoteSalesDiscount);

            //调拨单
            var changeWarehouseBuilder = modelBuilder.Entity<ChangeWarehouse>().ConfigBaseEntity().ConfigReceiptEntity();
            changeWarehouseBuilder.ToTable("change_warehouse");
            changeWarehouseBuilder.Property(x => x.goodsID).IsRequired().HasMaxLength(50);
            changeWarehouseBuilder.Property(x => x.goodsName).HasMaxLength(50);
            changeWarehouseBuilder.Property(x => x.outWarehouseID).HasMaxLength(50);
            changeWarehouseBuilder.Property(x => x.inWarehouseID).HasMaxLength(50);
            changeWarehouseBuilder.Property(x => x.quantity).HasPrecision(18, 4);
            changeWarehouseBuilder.Property(x => x.remark).HasMaxLength(1000);
            changeWarehouseBuilder.Ignore(x => x.InWarehouse).Ignore(x => x.OutWarehouse);

            //盘点
            var checkStockBuilder = modelBuilder.Entity<CheckStock>()
                .ConfigBaseEntity()
                .ConfigReceiptEntity()
                .ConfigWarehouse();

            checkStockBuilder.ToTable("check_stock");
            checkStockBuilder.Property(x => x.goodsID).IsRequired().HasMaxLength(50);
            checkStockBuilder.Property(x => x.goodsName).HasMaxLength(50);
            checkStockBuilder.Property(x => x.oldNum).IsRequired().HasPrecision(18, 4);
            checkStockBuilder.Property(x => x.newNum).IsRequired().HasPrecision(18, 4);
            checkStockBuilder.Property(x => x.remark).HasMaxLength(1000);
            checkStockBuilder.Ignore(x => x.Warehouse);
            checkStockBuilder.Ignore(x => x.GoodsInfo);

            //进出库（进退货）单
            var inOutwarehousingBuilder = modelBuilder.Entity<InOutWarehousing>()
                .ConfigBaseEntity()
                .ConfigReceiptEntity()
                .ConfigWarehouse();

            inOutwarehousingBuilder.ToTable("inOut_warehousing");
            inOutwarehousingBuilder.Property(x => x.goodsID).IsRequired().HasMaxLength(50);
            inOutwarehousingBuilder.Property(x => x.goodsName).HasMaxLength(50);
            inOutwarehousingBuilder.Property(x => x.quantity).IsRequired().HasPrecision(18, 4);
            inOutwarehousingBuilder.Property(x => x.price).IsRequired().HasPrecision(18, 4);
            inOutwarehousingBuilder.Property(x => x.inOrOut);
            inOutwarehousingBuilder.Property(x => x.curSalePrice).HasPrecision(18, 4);
            inOutwarehousingBuilder.Property(x => x.grossProfit).HasPrecision(18, 4);
            inOutwarehousingBuilder.Property(x => x.remark).HasMaxLength(1000);
            inOutwarehousingBuilder.Property(x => x.remarkMst).HasMaxLength(1000);
            inOutwarehousingBuilder.Ignore(x => x.GoodsInfo).Ignore(x => x.Warehouse).Ignore(x => x.Supplier);

            //促销-满减
            var promoteSalesFullCutBuilder = modelBuilder.Entity<PromoteSalesFullCut>()
                .ConfigBaseEntity()
                .ConfigReceiptEntity()
                .ConfigWarehouse();

            promoteSalesFullCutBuilder.ToTable("promoteSales_FullCut");
            promoteSalesFullCutBuilder.Property(x => x.fullAmount).IsRequired().HasPrecision(18, 4);
            promoteSalesFullCutBuilder.Property(x => x.cutAmount).IsRequired().HasPrecision(18, 4);
            //最大优惠
            promoteSalesFullCutBuilder.Property(x => x.maxDiscountAmout).IsRequired().HasPrecision(18, 4);
            promoteSalesFullCutBuilder.Property(x => x.priorityLevel);
            promoteSalesFullCutBuilder.Property(x => x.isMemberOnly);
            promoteSalesFullCutBuilder.Property(x => x.isIntegral);
            promoteSalesFullCutBuilder.Property(x => x.startTime).HasColumnType("datetime");
            promoteSalesFullCutBuilder.Property(x => x.endTime).HasColumnType("datetime");
            promoteSalesFullCutBuilder.Ignore(x => x.Warehouse)
                .Ignore(x => x.GoodsCategory)
                .Ignore(x => x.PromoteSalesFullCutCategory);

            //add by cl 20240311
            //满减关联分类 
            var fullCutCategoryBuilder = modelBuilder.Entity<PromoteSalesFullCutCategory>().ConfigBaseEntity();
            fullCutCategoryBuilder.ToTable("promoteSales_FullCut_category");
            fullCutCategoryBuilder.Property(x => x.fullCutID).IsRequired().HasMaxLength(100);
            fullCutCategoryBuilder.Property(x => x.categoryID).IsRequired().HasMaxLength(100);
            fullCutCategoryBuilder.Ignore(x => x.GoodsCategory).Ignore(x => x.PromoteSalesFullCut);

            //库存
            var warehouseBuilder = modelBuilder.Entity<Warehouse.Entity.Warehouse>().ConfigBaseEntity().ConfigNo();
            warehouseBuilder.ToTable("warehouse");
            warehouseBuilder.Property(x => x.warehouseName).IsRequired().HasMaxLength(50);
            warehouseBuilder.Property(x => x.No).IsRequired().HasMaxLength(50);
            warehouseBuilder.Property(x => x.remark).HasMaxLength(500);
            warehouseBuilder.Property(x => x.isEnable);

            //库存商品关联表
            var warehouseGoodsBuilder = modelBuilder.Entity<WarehouseGoods>()
                .ConfigBaseEntity()
                .ConfigWarehouse();

            warehouseGoodsBuilder.ToTable("warehouse_goods");
            warehouseGoodsBuilder.Property(x => x.goodsID).IsRequired().HasMaxLength(50);
            warehouseGoodsBuilder.Property(x => x.goodsNum).IsRequired().HasPrecision(18, 4);
            warehouseGoodsBuilder.Property(x => x.costPrice).HasPrecision(18, 4);
            warehouseGoodsBuilder.Property(x => x.costAmount).HasPrecision(18, 4);
            warehouseGoodsBuilder.Property(x => x.price).HasPrecision(18, 4).IsRequired();
            warehouseGoodsBuilder.Ignore(x => x.Warehouse);
            warehouseGoodsBuilder.Ignore(x => x.GoodsInfo);
            warehouseGoodsBuilder.Ignore(x => x.Supplier);
            warehouseGoodsBuilder.Ignore(x => x.GoodsMemberPrice);

            //批发销售发货退货单
            var warehouseInoutBuilder = modelBuilder.Entity<WholesaleInOut>()
                .ConfigBaseEntity()
                .ConfigReceiptEntity()
                .ConfigWarehouse();

            warehouseInoutBuilder.ToTable("wholesale_inOut");
            warehouseInoutBuilder.Property(x => x.customerID).HasMaxLength(50);
            warehouseInoutBuilder.Property(x => x.goodsID).IsRequired().HasMaxLength(50);
            warehouseInoutBuilder.Property(x => x.quantity).IsRequired().HasPrecision(18, 4);
            warehouseInoutBuilder.Property(x => x.price).IsRequired().HasPrecision(18, 4);
            warehouseInoutBuilder.Property(x => x.inOrOut);
            warehouseInoutBuilder.Property(x => x.remark).HasMaxLength(1000);
            warehouseInoutBuilder.Property(x => x.remarkMst).HasMaxLength(1000);
            warehouseInoutBuilder.Property(x => x.discountAmountMst).IsRequired().HasPrecision(18, 4).HasDefaultValue(0);
            warehouseInoutBuilder.Property(x => x.priceType);
            warehouseInoutBuilder
                .Ignore(x => x.Warehouse)
                .Ignore(x => x.WholesaleCustomer)
                .Ignore(x => x.GoodsInfo)
                .Ignore(x => x.GoodsMemberPrice);

            //批发收款单
            var warehouseReceiptBuilder = modelBuilder.Entity<WholesaleReceipt>().ConfigBaseEntity().ConfigReceiptEntity();
            warehouseReceiptBuilder.ToTable("wholesale_receipt");
            warehouseReceiptBuilder.Property(x => x.customerID).HasMaxLength(100);
            warehouseReceiptBuilder.Property(x => x.money).HasPrecision(18, 4);
            warehouseReceiptBuilder.Property(x => x.remark).HasMaxLength(1000);

            //批发客户表
            var wholeSaleCustomerBuilder = modelBuilder.Entity<WholesaleCustomer>().ConfigBaseEntity().ConfigNo();
            wholeSaleCustomerBuilder.ToTable("wholesale_customer");
            wholeSaleCustomerBuilder.Property(x => x.No).IsRequired().HasMaxLength(50);
            wholeSaleCustomerBuilder.Property(x => x.customerName).IsRequired().HasMaxLength(50);
            wholeSaleCustomerBuilder.Property(x => x.linkman).HasMaxLength(50);
            wholeSaleCustomerBuilder.Property(x => x.phone).HasMaxLength(50);
            wholeSaleCustomerBuilder.Property(x => x.receivingAddress).HasMaxLength(500);
            wholeSaleCustomerBuilder.Property(x => x.taxpayerNo).HasMaxLength(50);
            wholeSaleCustomerBuilder.Property(x => x.bankOfDeposit).HasMaxLength(50);
            wholeSaleCustomerBuilder.Property(x => x.bankCardNumber).HasMaxLength(50);
            wholeSaleCustomerBuilder.Property(x => x.contactNumber).HasMaxLength(50);

            #region add by cl
            //其他单
            var otherBillBuilder = modelBuilder.Entity<OtherBill>()
                .ConfigBaseEntity()
                .ConfigReceiptEntity()
                .ConfigWarehouse();

            otherBillBuilder.ToTable("other_bill");
            otherBillBuilder.Property(x => x.goodsID).IsRequired().HasMaxLength(50);
            otherBillBuilder.Property(x => x.goodsName).HasMaxLength(50);
            otherBillBuilder.Property(x => x.Num).IsRequired().HasPrecision(18, 4);
            otherBillBuilder.Property(x => x.inOrOut).IsRequired();
            otherBillBuilder.Property(x => x.remark).HasMaxLength(1000);
            otherBillBuilder.Ignore(x => x.Warehouse).Ignore(x => x.GoodsInfo);

            //次品单
            var defectiveBillBuilder = modelBuilder.Entity<DefectiveBill>()
                .ConfigBaseEntity()
                .ConfigReceiptEntity()
                .ConfigWarehouse();

            defectiveBillBuilder.ToTable("defective_bill");
            defectiveBillBuilder.Property(x => x.goodsID).IsRequired().HasMaxLength(50);
            defectiveBillBuilder.Property(x => x.goodsName).HasMaxLength(50);
            defectiveBillBuilder.Property(x => x.quantity).IsRequired().HasPrecision(18, 4);
            defectiveBillBuilder.Property(x => x.price).IsRequired().HasPrecision(18, 4);
            defectiveBillBuilder.Property(x => x.remark).HasMaxLength(1000);
            defectiveBillBuilder.Ignore(x => x.Warehouse).Ignore(x => x.GoodsInfo).Ignore(x => x.Supplier);
            #endregion

            //尾数取值规则表
            var rateruleBuilder = modelBuilder.Entity<RateRule>()
                .ConfigBaseEntity().ConfigReceiptEntity().ConfigWarehouse();
            rateruleBuilder.ToTable("rate_rule");
            rateruleBuilder
                .Ignore(x => x.Warehouse);
        }
    }
}
